package net.bitdixit.ywu.library;

import java.util.ArrayList;
import java.util.Arrays;

import net.bitdixit.ywu.preferences.AppPreferences;

import org.json.JSONObject;
import org.json.JSONTokener;

public class AsanaLibraryView
{
	public class ViewData implements  java.io.Serializable
	{
		private static final long serialVersionUID = 1L;
		private String         filter;
		private boolean        onlyStared;
		private String         lang;	
		
		public void setFilter(String filter)
		{
			this.filter = filter;
		}
		public void setOnlyStared(boolean onlyStared)
		{
			this.onlyStared = onlyStared;
		}
		public void setLang(String lang)
		{
			this.lang = lang;
		}
		public String getFilter()
		{
			return this.filter;
		}
		public boolean isOnlyStared()
		{
			return this.onlyStared;
		}
		public String getLang()
		{
			return this.lang;
		}
		public String serialize() 
		{
			try
			{
				JSONObject map = new JSONObject();
				if (filter!=null) map.put("filter", getFilter());
				map.put("stared",isOnlyStared());
				map.put("lang", getLang());
				return map.toString();
			} catch (Exception e)
			{
				e.printStackTrace();
				return "";
			}
		}
		public void unserialize(String serialized) 
		{
			try
			{
				
			JSONObject map = new JSONObject(new JSONTokener(serialized));
			setFilter(map.has("filter")?map.getString("filter"):null);
			setOnlyStared(map.getBoolean("stared"));
			setLang(map.getString("lang"));
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		}
	}
	
	private AsanaLibrary   asanaLibrary;
	private AppPreferences preferences;
	private Asana[]        viewAsanas;
	private ViewData       viewData;
	
	public AsanaLibraryView(AsanaLibrary asanaLibrary, AppPreferences preferences)
	{
		this.asanaLibrary = asanaLibrary;
		this.preferences = preferences;
		this.viewData = new ViewData();
		viewData.setFilter(null);
		viewData.setOnlyStared(false);
		viewData.setLang(AsanaLibrary.LANG_SK);
		apply();
	}
	public ViewData viewData()
	{
		return viewData;
	}
	
	public void apply()
	{
		if (!viewData.onlyStared)
		{
			if (viewData.filter==null)
		    {
				viewAsanas = asanaLibrary.getAllAsanas();
		    } else
		    {
				ArrayList<Asana> found = new ArrayList<Asana>();
				for (Asana asana : asanaLibrary.getAllAsanas())
				{
					
					if ((asana.getName(viewData.lang).toUpperCase())
						.contains(viewData.filter.toUpperCase()))
						found.add(asana);
				}
				viewAsanas=found.toArray(new Asana[found.size()]);	
		    }
		}
		else 
		{
			ArrayList<String> staredIds = preferences.getStaredAsanasList();
			ArrayList<Asana> stared = new ArrayList<Asana>();
			for (Asana asana : asanaLibrary.getAllAsanas())
				if (staredIds.contains(asana.getId())) stared.add(asana);
			viewAsanas=stared.toArray(new Asana[stared.size()]);
		}
		Arrays.sort(viewAsanas,new Asana.Comprare(viewData.lang));
	}
	public Asana get(String asanaId)
	{
		for (Asana asana : viewAsanas)
			if (asana.getId().equals(asanaId)) return asana;
		return null;
	}
	public Asana next(Asana asana, boolean forward)
	{
		int pos = 0;
		while (!viewAsanas[pos].getId().equals(asana.getId())) pos++;
		return viewAsanas[(viewAsanas.length+pos+(forward?1:-1))%viewAsanas.length];		
	}
	public Asana[] asanas()
	{
		return viewAsanas;
	}
	
}
